#pragma once
#include "const.h"
#include "MsgNode.h"
class CServer;
class CSession:public std::enable_shared_from_this<CSession>
{
public:
	CSession(boost::asio::io_context& ioc, CServer* server);
	~CSession();
	boost::asio::ip::tcp::socket& GetSocket();
	void Start();
	//std::string& GetUid();
	//GetUid()
	std::string& GetSessionId();
	int GetUserId();
	void SetUserId(int uid);
	void Send(std::string msg, short msgid);


private:
	boost::asio::ip::tcp::socket _socket;
	std::string _session_id;
	int _user_uid;
	CServer* _server;
	bool _b_close;
	std::mutex _mutex;
	bool _b_parse;
	char _data[MAX_LENGTH];

	std::shared_ptr<MsgNode>_recv_head_node;
	std::shared_ptr<RecvNode>_recv_msg_node;
	std::queue<std::shared_ptr<SendNode>>_send_que;

	void AsyncReadHead(int total_len);
	void AsyncReadBody(int total_len);
	void AsyncReadLen(std::size_t read_len, std::size_t total_len, std::function<void(const boost::system::error_code&, std::size_t)> handler);
	void AsyncReadFull(std::size_t total_len, std::function<void(const boost::system::error_code&, std::size_t)>handler);
	void Close();
	void HandleWrite(const boost::system::error_code& error, std::shared_ptr<CSession>self);

};

class LogicNode {
public:
	LogicNode(std::shared_ptr<CSession>, std::shared_ptr<RecvNode>);

	std::shared_ptr<CSession>_session;
	std::shared_ptr<RecvNode>_recvnode;
};